package com.leisai.manager.controller.system;

import com.alibaba.fastjson.JSONArray;
import com.leisai.manager.common.utils.DateUtils;
import com.leisai.manager.dto.IndustryDTO;
import com.leisai.manager.dto.ReportDTO;
import com.leisai.manager.response.PageDataResult;
import com.leisai.manager.service.ReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("report")
public class ReportController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ReportService reportService;
    /**
    * @Description: 进入报表填写页面
    * @Param: []
    * @return: java.lang.String
    * @Author: jim
    * @Date: 2021/11/27
    */
    @RequestMapping("reportManage")
    public String reportManage(IndustryDTO industryDTO, Model model) {
        logger.info("进入报表汇总");
        //将报表初始化
        ReportDTO reportDTO = new ReportDTO();
        reportDTO.setIndustryId(industryDTO.getIndustryId());
        reportDTO.setYear(DateUtils.getFormatTime(new Date(),"yyyy"));
        String pids = industryDTO.getPids();
        if (pids.split(",").length==3){    //细分行业才需要初始化
            reportService.initReport(reportDTO);
        }else {
            model.addAttribute("msg","细分行业才需要填写报表");
        }

        model.addAttribute("industry",industryDTO);
        return "report/reportManage";
    }

    /**
     *
     * 功能描述: 获取行业列表
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2021/11/30 10:30
     */
    @RequestMapping(value = "/reportList", method = RequestMethod.POST)
    @ResponseBody
    public PageDataResult reportList(@RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize, ReportDTO reportSearch){
        logger.info("获取报表列表");
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 20;
            }
            // 获取服务类目列表
            pdr = reportService.getReportList(pageNum ,pageSize,reportSearch);
            logger.info("报表列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("报表列表查询异常！", e);
        }
        return pdr;
    }


    @RequestMapping("reportView")
    public String reportView(ReportDTO reportDTO , Model model) {
        logger.info("进入报表分析");
        Integer industryId = reportDTO.getIndustryId();
        String year = reportDTO.getYear();
        String industryName = reportDTO.getIndustryName();
        if (industryId == null){
            industryId = 1;
            industryName = "总公司";
        }
        model.addAttribute("industryId",industryId);
        model.addAttribute("industryName",industryName);
        model.addAttribute("year",year);
        return "report/reportView";
    }
    /**
     *
     * 功能描述: 获取行业列表
     *
     * @param:
     * @return:
     * @auther: jim
     * @date: 2021/11/30 10:30
     */
    @RequestMapping(value = "/getReportDate", method = RequestMethod.POST)
    @ResponseBody
    public JSONArray getReportDate(ReportDTO reportDTO) {
        logger.info("获取报表列表");
        JSONArray jsonArray = reportService.getIndustryReport(reportDTO);
        return jsonArray;

    }
}
